package indi.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.woniu.entity.Paper;
import indi.woniu.field.Tables;
import indi.woniu.mapper.PaperMapper;
import indi.woniu.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: GaN601
 * @Date: 2021/12/15 14:13
 * @Description:
 */
@Service
public class PaperServiceImpl implements PaperService {
    private PaperMapper paperMapper;
    
    
    @Autowired
    public void setPaperMapper(final PaperMapper paperMapper) {
        this.paperMapper = paperMapper;
    }
    
    @Override
    public List<Paper> selectAll() {
        
        return paperMapper.selectAllPaper();
    }
    
    /**
     * 分页查询
     *
     * @return 包装好的分页对象
     */
    @Override
    public PageInfo<?> getPageData(Integer pageSum, Integer pageSize, Map<String, Object> fields) {
        PageHelper.startPage(pageSum, pageSize);
        return new PageInfo<>(paperMapper.selectByField(Tables.PAPER, fields));
    }
    
    /**
     * 查询某个字段, 使用分组使其不重复
     */
    @Override
    public List<String> selectByGroup(Tables table,final String field) {
        
        return paperMapper.selectByGroup(Tables.PAPER, field);
    }
    
    /**
     * 查询多个字段, 使用分组使其不重复
     *
     * @return 结果集
     */
    @Override
    public Map<String, List<String>> selectByGroup(final Collection<String> fields) {
        Map<String, List<String>> maps = new HashMap<>(8);
        for (String element : fields) {
            maps.put(element, paperMapper.selectByGroup(Tables.PAPER, element));
        }
        return maps;
    }
    
    
    /**
     * 多表查询时, 进行多次查询并封装进 Map 或者实体类中
     * 未完成 需要关联表
     */
    @Override
    public List<Map<String, Object>> selectAllByResultPacking(Map<String, Object> fields) {
        
        return paperMapper.selectByField(Tables.PAPER, fields);
    }
    
    @Override
    public List<Map<String, Object>> selectByFields(final Map<String, Object> fields) {
        return paperMapper.selectByField(Tables.PAPER, fields);
    }
    
    /**
     * 更新一行数据, 需要将 paper 的id 设为id
     *
     * @param id    id
     * @param paper 试卷
     * @return 影响行数
     */
    @Override
    public Integer update(final Integer id, final Map<String, Object> paper) {
        paper.put("id", id);
        return paperMapper.insert(Tables.PAPER, paper);
    }
    
    /**
     * 删除一行数据
     *
     * @param id id
     * @return 影响行数
     */
    @Override
    public Integer delete(final Integer id) {
        return paperMapper.delete(Tables.PAPER, id);
    }
}
